// file2array.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


int _tmain(int argc, char* argv[])
{
	FILE *f;
	size_t length;
	unsigned char *buf;
	char *string;
	char *buf2;
	size_t ptr;
	printf("File to C Array converter V0.1 by Peterbjornx\n");
	if (argc < 3){
		printf("Usage: file2array <filename> <c filename> <arrayname>\n");
		return 1;
	}
	f = fopen(argv[1],"rb");
	if (f == NULL){
		printf("Error: Can not find input file %s\n",argv[1]);
		return 1;
	}
	fseek(f,0,SEEK_END);
	length = ftell(f);
	rewind(f);
	buf = (unsigned char *) malloc(length);
	//buf2 = (char *) malloc(10);
	//buf2[0] = '\0';
	//string = (char *) malloc((length * 17)+80);//80 characters for C name, type and syntax-required symbols
	//string[0] = '\0';
	length = fread(buf,sizeof(unsigned char),length,f);
	fclose(f);
	f = fopen(argv[2],"w");
	if (f == NULL){
		printf("Error: Can not create output file %s\n",argv[2]);
		return 1;
	}
	fprintf(f,"uint8_t %s[%i] = {",argv[3],length);
	for (ptr = 0;ptr < length;ptr++){
		if (ptr == 0)
			fprintf(f,"(uint8_t) 0x%X",buf[ptr] & 0xFF);
		else
			fprintf(f,", (uint8_t) 0x%X",buf[ptr] & 0xFF);
	}
	fprintf(f,"};\n");
	///fclose(f);
	return 0;
}

